﻿using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TracNghiemV1.Database;

namespace TracNghiemV1.DAO
{
    class QuestionDao
    {
        private static TestDataDataContext m_dataContext = null;

        public static IList<QUESTION> GetQuestionList()
        {
            return DataContextDao.GetInstance().QUESTIONs.ToList();
        }

        public static void AddQuestion(QUESTION newQuestion)
        {
            m_dataContext = DataContextDao.GetInstance();

            m_dataContext.QUESTIONs.InsertOnSubmit(newQuestion);
            m_dataContext.SubmitChanges();
           
        }

        public static void DeleteQuestion(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var question = m_dataContext.QUESTIONs.Single(u => u.QUESTION_ID == questionId);
            m_dataContext.QUESTIONs.DeleteOnSubmit(question);
            m_dataContext.SubmitChanges();
        }

        public static void ModifyQuestion(QUESTION qt)
        {
            m_dataContext = DataContextDao.GetInstance();

            var course = m_dataContext.QUESTIONs.Single(u => u.QUESTION_ID == qt.QUESTION_ID);

            course.QUESTION_DESCRIPTION = qt.QUESTION_DESCRIPTION;
            course.QUESTION_TYPE = qt.QUESTION_TYPE;

            m_dataContext.SubmitChanges();
           
        }

        public static QUESTION FindQuestionWithQuestionId(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();
            return m_dataContext.QUESTIONs.Single(u => u.QUESTION_ID == questionId);
        }

        public static IList<QUESTION> FindQuestionListWithDescription(string name, string userId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var questions = from p in m_dataContext.QUESTIONs
                            where SqlMethods.Like(p.QUESTION_DESCRIPTION, "%" + name + "%") && p.USER_ID == userId
                            select p;
            return questions.ToList();
        }

        public static IList<TOPIC> FindTopicListWithQuestionId(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var questions = from q in m_dataContext.QUESTION_TOPIC_MAPs
                            from r in m_dataContext.TOPICs
                            where q.QUESTION_ID == questionId && q.TOPIC_ID == r.TOPIC_ID
                            select r;
            return questions.ToList();
        }

        public static IList<QUESTION_ITEM_INFO> FindQuestionItemInfoWithQuestionId(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var questions = from q in m_dataContext.QUESTION_ITEMs
                            from r in m_dataContext.QUESTION_ITEM_INFOs
                            where q.QUESTION_ID == questionId && r.ITEM_ID == q.ITEM_ID
                            select r;
            return questions.ToList();
        }

        public static IList<QUESTION_SOLUTION> FindQuestionSolutionListWithQuestionId(int questionId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var questions = from q in m_dataContext.QUESTION_SOLUTIONs
                            where q.QUESTION_ID == questionId
                            select q;
            return questions.ToList();
        }
    }
}
